package com.wash.framework.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class UploaderServlet extends HttpServlet
{
  private static final long serialVersionUID = 1L;
  String repositoryPath;
  String uploadPath;

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setCharacterEncoding("UTF-8");
    Integer schunk = null;
    Integer schunks = null;
    String name = null;
    BufferedOutputStream outputStream = null;
    if (ServletFileUpload.isMultipartContent(request))
      try {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(1024);
        factory.setRepository(new File(this.repositoryPath));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        upload.setSizeMax(5242880L);
        List<FileItem> items = upload.parseRequest(request);

        String newFileName = null;
        for (FileItem item : items) {
          if (!item.isFormField()) {
            name = item.getName();
            newFileName = UUID.randomUUID().toString().replace("-", "").concat(".").concat(FilenameUtils.getExtension(name));
            if (name != null) {
              String nFname = newFileName;
              if (schunk != null) {
                nFname = schunk + "_" + name;
              }
              File savedFile = new File(this.uploadPath, nFname);
              item.write(savedFile);
            }
          }
          else {
            if (item.getFieldName().equals("chunk")) {
              schunk = Integer.valueOf(Integer.parseInt(item.getString()));
            }
            if (item.getFieldName().equals("chunks")) {
              schunks = Integer.valueOf(Integer.parseInt(item.getString()));
            }
          }
        }

        if ((schunk != null) && (schunk.intValue() + 1 == schunks.intValue())) {
          outputStream = new BufferedOutputStream(new FileOutputStream(new File(this.uploadPath, newFileName)));

          for (int i = 0; i < schunks.intValue(); i++) {
            File tempFile = new File(this.uploadPath, i + "_" + name);
            byte[] bytes = FileUtils.readFileToByteArray(tempFile);
            outputStream.write(bytes);
            outputStream.flush();
            tempFile.delete();
          }
          outputStream.flush();
        }
        response.getWriter().write("{\"status\":true,\"newName\":\"" + newFileName + "\"}");
      } catch (FileUploadException e) {
        e.printStackTrace();
        response.getWriter().write("{\"status\":false}");
      } catch (Exception e) {
        e.printStackTrace();
        response.getWriter().write("{\"status\":false}");
      } finally {
        try {
          if (outputStream != null)
            outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
  }

  public void init(ServletConfig config)
    throws ServletException
  {
    this.repositoryPath = FileUtils.getTempDirectoryPath();
    this.uploadPath = config.getServletContext().getRealPath(config.getInitParameter("uploadPath"));
    File up = new File(this.uploadPath);
    if (!up.exists())
      up.mkdir();
  }
}